<template>
  <tl-sub-page :render="render" :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="800px" @open="open" @close="close" append-to-body>
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
      <el-row>
        <el-col :span="12">
          <el-form-item label="任务负责人" prop="responsibleusercode" :rules="rules.validateRequire">
            <tl-ref-user  v-model="model.responsibleusercode" :initDisplayData.sync="model.responsibleusername" dataType="code" :singleSelect="true" @confirm="confirmUser" :disabled="editByResponsibleDisabled || disableEditSubmit"></tl-ref-user>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="任务名称" prop="name" :rules="rules.validateRequire">
            <el-input v-model="model.name" :maxlength="100" show-word-limit placeholder="请输入任务名称" :disabled="editByResponsibleDisabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="任务描述" prop="introduction">
            <el-input v-model="model.introduction" type="textarea" :rows="2" :maxlength="1000" show-word-limit placeholder="请输入任务描述" :disabled="editByResponsibleDisabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="计划开始时间" prop="planbegintime" :rules="rules.validateRequire">
            <el-date-picker v-model="model.planbegintime" type="date" placeholder="计划开始时间" value-format="yyyy-MM-dd 00:00:00"  @change="compareTime(model.planbegintime, model.planendtime, 0)" :picker-options="pickerOptions" :options="1"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划结束时间" prop="planendtime" :rules="rules.validateRequire">
            <el-date-picker v-model="model.planendtime" type="date" placeholder="计划结束时间" value-format="yyyy-MM-dd 23:59:59"  @change="compareTime(model.planbegintime, model.planendtime, 1)" :picker-options="pickerOptions"></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="预估工时" prop="planhours" :rules="[ ...rules.validateRequire, ...checkPositiveInt ]">
            <el-input v-model="model.planhours" type="number" style="width: 100%;" :disabled="editByResponsibleDisabled"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="任务类型" prop="type" :rules="rules.validateRequire">
            <el-select v-model="model.type" filterable :disabled="editTaskType || editByResponsibleDisabled">
              <el-option v-for="item in codeModel.taskTypeList" :key="item.code" :label="item.name" :value="parseInt(item.code)" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" v-if="!disableEditSubmit">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doLocalSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
  </tl-sub-page>
</template>

<script>
  /** mixins */
  import { mixin_edit } from '@tapui/mixins/mixin_edit'
  /** 子组件 */
  /** 工具类 */
  import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
  import { checkPositiveInt } from '@/utils/numberValidate'
  import { TASK_RUNNING_STATE, TASK_TYPE, CBO_YES_NO, TASK_SOURCE, ADD_TASK_SOURCE, TASK_AUTO_COMPLETE, TASK_RELATION, TASK_STATE } from '../../../const/const'
  /** VUE配置 */
  export default {
    mixins: [mixin_edit],
    components: { },
    data() {
      return {
        moduleTitle: 'WBS任务表',
        urlPrefix: '/cbb/pmTask/',
        url: {
          // 额外的url
          queryCodeList: '/cbb/pmTask/dataCodeList'
        },
        checkPositiveInt: [
          // 正整形校验
          { validator: checkPositiveInt }
        ],
        pickerOptions: {
          disabledDate: this.disabledDate
        },
        isQueryInitData: true
      }
    },
    props: {
      selectInfo: {
        type: Object,
        default: () => {
          return {}
        }
      },
      editTaskInfo: {
        type: Object,
        default: () => {
          return {}
        }
      },
      addSourceType: {
        type: Number,
        default: ADD_TASK_SOURCE.SameLevel
      },
      haveSpecialTask: {
        type: Boolean,
        default: false
      },
      render: {
        type: String
      }
    },
    computed: {
      // 是否增加子级任务
      isNextLevelAdd() {
        return this.addSourceType === ADD_TASK_SOURCE.NextLevel
      },
      // 节点任务且需要填写单据
      isNodeSpecialTask() {
        return this.selectInfo.isWriteDoc && this.selectInfo.parentcode === '0'
      },
      // 是否不可编辑任务类型，如果是子任务，且是节点任务且需要填写单据，且已经有特殊任务了
      editTaskType() {
        return this.haveSpecialTask
      },
      // 编辑时是否是负责人编辑
      editByResponsibleDisabled() {
        return this.isAdd === false && this.user.code === this.model.responsibleusercode && this.model.deliverystatus === CBO_YES_NO.Yes
      }
    },
    methods: {
      compareTime(time1, time2, type) {
        if (time1 && time2) {
          let t1 = new Date(time1)
          let t2 = new Date(time2)
          if (t1 > t2) {
            if (type === 0) {
              this.model.planbegintime = ''
              this.$tap.alert('计划开始时间开始时间不应该大于计划结束时间')
            } else {
              this.model.planendtime = ''
              this.$tap.alert('计划结束时间结束时间不应该小于计划开始时间')
            }
          }
        }
      },
      loaded() {
        // 页面加载完毕执行
      },
      open() {
        this.model = {}
        this.$nextTick(() => {
          this.$refs.form.clearValidate()
          if (this.isAdd === true) {
            this.title = '添加' + this.moduleTitle
            // 需要添加重复判断
            if (this.$store.getters.param.isAvoidRepeat === true) {
              this.queryBillUid()
            }
            if (this.isQueryInitData === true) {
              // 后台进行数据初始化
              this.queryInitData().then((_) => {
                this.assignDefaultValue()
                this.$refs.form.clearValidate()
                this.loaded()
              })
            } else {
              // 前台进行数据初始化
              this.$set(this.model, 'enabled', 1)
              this.$set(this.model, 'sortcode', 0)
              this.assignDefaultValue()
              this.$refs.form.clearValidate()
              this.loaded()
            }
          } else if (this.disableEditSubmit === false) {
            this.title = '编辑' + this.moduleTitle
            this.model = JSON.parse(JSON.stringify(this.editTaskInfo))
            this.loaded()
          } else {
            this.title = '查看' + this.moduleTitle
            this.model = JSON.parse(JSON.stringify(this.editTaskInfo))
            this.loaded()
          }
        })
      },
      assignDefaultValue() {
        // 选中信息赋值
        // 增加下级任务，使用选择的任务的code做parentcode
        if (this.isNextLevelAdd) {
          this.$set(this.model, 'parentcode', this.selectInfo.code)
          this.$set(this.model, 'level', this.selectInfo.level + 1)
        } else {
          this.$set(this.model, 'parentcode', this.selectInfo.parentcode)
          this.$set(this.model, 'level', this.selectInfo.level)
          this.$set(this.model, 'sortcode', this.selectInfo.sortcode)
        }
        this.$set(this.model, 'projectuucode', this.selectInfo.projectuucode)
        this.$set(this.model, 'wbscode', this.selectInfo.wbscode)
        if (this.selectInfo.parentcode === '0') {
          this.$set(this.model, 'origintaskcode', this.selectInfo.code)
        } else {
          this.$set(this.model, 'origintaskcode', this.selectInfo.origintaskcode)
        }
        this.$set(this.model, 'status', TASK_STATE.Notstart)
        this.$set(this.model, 'dispstatus', '未开始')
        this.$set(this.model, 'flowid', this.selectInfo.flowid)
        this.$set(this.model, 'flowinsid', this.selectInfo.flowinsid)
        this.$set(this.model, 'nodeid', this.selectInfo.nodeid)
        this.$set(this.model, 'nodeinsid', this.selectInfo.nodeinsid)
        this.$set(this.model, 'round', this.selectInfo.round)
        this.$set(this.model, 'responsibleusercode', this.selectInfo.responsibleusercode)
        this.$set(this.model, 'responsibleusername', this.selectInfo.responsibleusername)
        // 字段默认赋值
        this.$set(this.model, 'effectiveness', TASK_RUNNING_STATE.Valid)
        this.$set(this.model, 'derived', CBO_YES_NO.No)
        this.$set(this.model, 'tasksource', TASK_SOURCE.Artificial)
        this.$set(this.model, 'deliverystatus', CBO_YES_NO.No)
        this.$set(this.model, 'autocomplete', TASK_AUTO_COMPLETE.LABOUR)
        this.$set(this.model, 'relation', TASK_RELATION.COUNTERSIGN)
        this.$set(this.model, 'type', TASK_TYPE.GENERAL)
        this.$set(this.model, 'currently', CBO_YES_NO.Yes)
        this.$set(this.model, 'source', TASK_SOURCE.Artificial)
        this.$set(this.model, 'children', [])
        this.$set(this.model, 'isAdd', CBO_YES_NO.Yes)
        // 额外前台判断参数
        this.$set(this.model, 'nodetype', this.selectInfo.nodetype)
        this.$set(this.model, 'approvalparam', this.selectInfo.approvalparam)
      },
      confirmUser(selectedData) {
      },
      queryData(id) {
        if (!this.url.load) {
          this.$tap.alert('请设置load地址!', 'warning')
          return
        }
        return getAction(this.url.load, { id: id }).then((res) => {
          if (res.success) {
            this.model = res.result
            this.$nextTick(() => {
              this.$refs.form.clearValidate()
            })
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
      },
      disabledDate(time) {
        if (this.selectInfo.planbegintime && this.selectInfo.planendtime) {
          let t1 = null
          let t2 = null
          t1 = new Date(this.selectInfo.planbegintime)
          if (this.isNextLevelAdd || this.model.parentcode) {
            t2 = new Date(this.selectInfo.planendtime)
          }
          return time < t1 || time > t2
        } else {
          return false
        }
      },
      doLocalSubmitEdit() {
        if (this.model.name) {
          if (this.model > 1) {
            this.$set(this.model, 'taskname', `第${this.model.round}轮${this.model.name}`)
          } else {
            this.$set(this.model, 'taskname', this.model.name)
          }
        }
        this.$refs.form.validate((valid) => {
          if (valid) {
            if (this.isAdd) {
              this.$emit('newTaskInfo', this.model, this.selectInfo.id)
            } else {
              this.$emit('editTaskInfo', this.model)
            }
            this.visible = false
          } else {
            return false
          }
        })
      }
    },
    created() {
    }
  }
</script>
<style lang="scss" scoped>
  /deep/ input::-webkit-inner-spin-button {
    -webkit-appearance: none !important;
  }

  /deep/ input::-webkit-outer-spin-button {
    -webkit-appearance: none !important;
  }

  input[type="number"] {
    -moz-appearance: textfield;
  }
</style>
